package br.com.foxline.atlasmobile;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import br.com.foxline.atlasmobile.adapter.ListCheckAdapter;
import br.com.foxline.atlasmobile.request.RequestWebJSON;
import br.com.foxline.atlasmobile.util.ConnectionDetector;
import br.com.foxline.atlasmobile.util.UtilConnection;

public class ExportActivity extends Activity {

	private ListView listView;
	private ConnectionDetector cd;
	private ListCheckAdapter arrayAdapter;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_export);

		cd = new ConnectionDetector(getApplicationContext());
		
		listView = (ListView) findViewById(R.id.listViewExport);

		TextView emptyView = (TextView) findViewById(android.R.id.empty);
		emptyView.setText(R.string.empty_export);

		listView.setEmptyView(emptyView);
		
		arrayAdapter = new ListCheckAdapter(getApplicationContext());
		
		listView.setAdapter(arrayAdapter);
		
		((Button) findViewById(R.id.expData))
				.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						exportDialog();
					}
				});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_export, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT).show();
		switch (item.getItemId()) {
		case R.id.sync_export:
			
			AlertDialog dialog;

			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setPositiveButton("Sim", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					exportFirstTen();
				}
			});

			builder.setNegativeButton("Não", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {

				}
			});

			builder.setMessage("Deseja enviar os dados ?").setTitle("Enviar Dados");

			dialog = builder.create();
			dialog.show();
			
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	public void exportDialog() {

		AlertDialog dialog;

		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setPositiveButton("Sim", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				export();
			}
		});

		builder.setNegativeButton("Não", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {

			}
		});

		builder.setMessage("Deseja enviar os dados ?").setTitle("Enviar Dados");

		dialog = builder.create();
		dialog.show();
	}

	private void export() {
		if (cd.isConnectingToInternet()) {
			System.gc();
			RequestWebJSON json = new RequestWebJSON(this, arrayAdapter);
			json.execute(arrayAdapter.getListChecked());
			
		} else {
			showAlertDialog(
					getApplicationContext(),
					"Sem conexão",
					"Para efetuar a exportação dos dados, conecte-se à internet",
					false);
		}
	}

	private void exportFirstTen() {
		if (cd.isConnectingToInternet()) {
			System.gc();
			SharedPreferences prefs = getSharedPreferences(UtilConnection.APP_SHARED_PREFS, 0);
			int qtd = prefs.getInt(UtilConnection.QTD_IMOVEIS_SINCRONIZADOS_SIMULTANEAMENTE, UtilConnection.QTD_IMOVEIS_SINCRONIZADOS_SIMULTANEAMENTE_DEFAULT);
			RequestWebJSON json = new RequestWebJSON(this, arrayAdapter);
			json.execute(arrayAdapter.getFirstListChecked(qtd));
			
		} else {
			showAlertDialog(
					getApplicationContext(),
					"Sem conexão",
					"Para efetuar a exportação dos dados, conecte-se à internet",
					false);
		}
	}
	
	public void showAlertDialog(Context context, String title, String message,
			Boolean status) {
		
		AlertDialog dialog;

		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("Sem conexão").setTitle("Erro");
		
		dialog = builder.create();
		dialog.setButton("OK", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
			}
		});
		dialog.show();
		

	}	
}
